package com.imti.mall.product.controller;

import com.imti.mall.common.Api.CommonPage;
import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.model.pms.pojo.PmsBrand;
import com.imti.mall.model.pms.vo.PmsBrandVo;
import com.imti.mall.product.service.PmsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: PmsBrandController
 * Description:
 * date: 2022/9/13 11:48
 *
 * @author: youhun
 * @version:
 * @since JDK 1.8
 */
@RestController
@Api(tags = "商品品牌管理")
@RequestMapping("/api/v1/product/brand")
public class PmsBrandController {
    
    @Autowired
    private PmsBrandService pmsBrandService;

    @ApiOperation("添加商品品牌数据")
    @PostMapping(value = "/addPmsBrand")
    public CommonResult addPmsBrand(@RequestBody PmsBrand pmsBrand) {
        boolean req = pmsBrandService.addPmsBrand(pmsBrand);
        if(req){
            return CommonResult.success("添加成功");
        }else {
            return CommonResult.failed("添加失败");
        }

    }

    @ApiOperation("根据ID修改商品分类数据")
    @PostMapping (value = "/updatePmsBrandById")
    public CommonResult updatePmsBrandById(PmsBrandVo pmsBrandVo) {
        long[] ids=pmsBrandVo.getIds();
        boolean req =false;
            for (int i=0;i<ids.length;i++){
                req =false;
                pmsBrandVo.setId(ids[i]);
                req = pmsBrandService.updatePmsBrandById(pmsBrandVo);
        }
        if(req){
            return CommonResult.success("修改成功");
        }else {
            return CommonResult.failed("修改失败");
        }

    }


    @ApiOperation("修改商品分类数据")
    @PostMapping (value = "/updatePmsBrand")
    public CommonResult updatePmsBrand(@RequestBody PmsBrandVo pmsBrandVo) {
        boolean req =false;
            req = pmsBrandService.updatePmsBrandById(pmsBrandVo);
        if(req){
            return CommonResult.success("修改成功");
        }else {
            return CommonResult.failed("修改失败");
        }

    }
    /**
     * 根据ID删除商品分类数据
     * @param Id
     * @return
     */
    @ApiOperation("根据ID删除商品分类数据")
    @GetMapping(value = "/deletePmsBrandById/{Id}")
    public CommonResult detail(@PathVariable("Id") Integer Id) {
        boolean req = pmsBrandService.deletePmsBrandById(Id);
        if(req){
            return CommonResult.success("删除成功");
        }else {
            return CommonResult.failed("删除失败");
        }

    }

    /**
     * 查询商品分类列表
     * @param pmsBrand
     * @param pageSize
     * @param pageNum
     * @return
     */
    @ApiOperation(value = "查询商品品牌")
    @PostMapping(value = "/findPmsBrand" )
    public CommonResult findPmsBrand
    (PmsBrand pmsBrand,
     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Map<String,Object> map=new HashMap<String,Object>();
        List<PmsBrand> pmsBrandList = pmsBrandService.findPmsBrand(pmsBrand,pageSize, pageNum);
        Integer total=pmsBrandService.getPmsBrand(pmsBrand);
        Integer totalPage= total/pageSize + 1;
        map.put("pmsBrandList",CommonPage.restPage(pmsBrandList));
        map.put("total",total);
        map.put("totalPage",totalPage);
        map.put("pageSize",pageSize);
        return CommonResult.success(map);
    }

    /**
     * 查询商品分类详情
     * @param Id
     * @return
     */
    @ApiOperation("根据ID获取商品分类详情")
    @GetMapping(value = "/getPmsBrandById/{Id}")
    public CommonResult<PmsBrand> getPmsBrandById(@PathVariable("Id") Integer Id) {
        PmsBrand PmsBrand = pmsBrandService.getPmsBrandById(Id);
        if(PmsBrand!=null){
            return CommonResult.success(PmsBrand);
        }else {
            return CommonResult.failed("未查询到信息");
        }

    }


    /**
     * 查询商品分类详情
     * @param Id
     * @return
     */
    @GetMapping(value = "/getPmsBrandByIdTo/{Id}")
    @ApiOperation("查询商品分类详情APP")
    public PmsBrand getPmsBrandByIdTo(@PathVariable("Id") Integer Id) {
        PmsBrand pmsBrand = pmsBrandService.getPmsBrandById(Id);
            return pmsBrand;




    }



}
